//
//  CorgiWelcomeViewController.swift
//  weibo
//
//  Created by Corgi on 17/3/23.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
import SDWebImage

private let iconWidth: CGFloat = 85
private let bottomMargin: CGFloat = 180

class CorgiWelcomeViewController: UIViewController {
    
    //懒加载控件
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", textColor: UIColor.darkGray, fontSize: 15)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    private func setupUI() {
        
        view.addSubview(iconView)
        
        view.addSubview(welcomeLabel)
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-180)
            make.height.width.equalTo(iconWidth)
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
        
        welcomeLabel.alpha = 0
        
        iconView.layer.cornerRadius = iconWidth * 0.5
        
        iconView.layer.masksToBounds = iconWidth > 0
        
        iconView.sd_setImage(with: CorgiUserAccountViewModel.shared.iconURL, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    func startAnimation() {
        
        let offset = -(screenHeight - bottomMargin - iconWidth)
        
        iconView.snp.updateConstraints { (make) in
            make.bottom.equalToSuperview().offset(offset)
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 0.4, animations: {
                
                self.welcomeLabel.alpha = 1
                
            }, completion: { (_) in
                
                //UIApplication.shared.keyWindow?.rootViewController = CorgiTabBarController()
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SWITCHROOTVIEWCONTROLLERNOTI), object: "welcomeViewController")
                
            })
        }
    }
    
}
